package com.example.service;

import com.example.entity.ImageFiles;
import com.example.entity.MriImages;
import com.example.mapper.ImageFilesMapper;
import com.example.mapper.MriImagesMapper;

import com.example.utils.GsonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO MRI影像表业务处理
 **/
@Service
public class MriImagesService {

    @Autowired
    private MriImagesMapper mriImagesMapper;

    @Autowired
    private ImageFilesMapper imageFilesMapper;


    /**
     * 新增
     */
    @Transactional
    public int add(MriImages mriImages) {
        return mriImagesMapper.insert(mriImages);

//        String seed = mriImages.getSeed();
//        // 获取文件的实际路径
//        String realFilePath = filePath + seed +  "_" + mriImages.getImageName() + ".nii";
//        //System.out.println(realFilePath);
//        niiFileProcessingService.processNiiFile(realFilePath, filePath);
//
//        // 插入 MRI 图像记录
//        mriImagesMapper.insert(mriImages);
//
//        // 生成 PNG 列表
//        List<List<String>> lists = niiFileProcessingService.saveGeneratePngs(mriImages.getImageName(),seed);
//
//        // 定义视图类型
//        String[] views = {"Axial", "Coronal", "Sagittal"};
//
//        // 遍历并插入 ImageFiles 数据
//        for (int i = 0; i < views.length; i++) {
//            insertImageFiles(mriImages.getImageName(), lists.get(i), views[i], seed);
//        }
    }

    /**
     * 封装的插入 ImageFiles 数据的辅助方法
     */
    private void insertImageFiles(String imageName, List<String> fileList, String viewType, String seed) {
        ImageFiles imageFiles = new ImageFiles();
        imageFiles.setNiiFileName(imageName);
        imageFiles.setFileName(GsonUtil.listToJson(fileList));
        imageFiles.setViewType(viewType);
        imageFiles.setSeed(seed);
        imageFilesMapper.insert(imageFiles);
    }


    /**
     * 删除
     */
    public void deleteById(Integer id) {
        mriImagesMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            mriImagesMapper.deleteById(id);
        }
    }

    /**
     * 修改(根据id)
     */
    public void updateById(MriImages mriImages) {
        mriImagesMapper.updateById(mriImages);
    }

    /**
     * 修改(根据seed)
     * @param seed
     * @param label
     */
    public void updateBySeed(String seed, String label) {
        mriImagesMapper.updateBySeed(seed, label);
    }

    /**
     * 根据ID查询
     */
    public MriImages selectById(Integer id) {
        return mriImagesMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<MriImages> selectAll(MriImages mriImages) {
        return mriImagesMapper.selectAll(mriImages);
    }

    /**
     * 分页查询
     */
    public PageInfo<MriImages> selectPage(MriImages mriImages, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MriImages> list = mriImagesMapper.selectAll(mriImages);
        return PageInfo.of(list);
    }

    public MriImages selectByFileNameAndUserId(String fileName, Integer userId) {
        return mriImagesMapper.selectByFileNameAndUserId(fileName, userId);
    }

    public List<List<String>> getPngList(String seed, String imageName) {
        List<ImageFiles> imageFiles = imageFilesMapper.selectBySeedAndFileName(seed, imageName);
        List<List<String>> lists = new ArrayList<>();
        for (ImageFiles imageFile : imageFiles) {
            lists.add(GsonUtil.strToList(imageFile.getFileName()));
        }
        return lists;
    }

    public String getDownloadUrl(String imageId) {
        return mriImagesMapper.selectById(Integer.parseInt(imageId)).getImagePath();
    }
}